#ifndef COMPRESSOR_MOTOR_HPP
#define COMPRESSOR_MOTOR_HPP

#include <cstdint>

class CompressorMotor {

public:
    class Adapter {
    public:
        virtual bool open(void) = 0;
        virtual bool close(void) = 0;
        virtual unsigned int read(void *buffer,unsigned int size) = 0;
        virtual unsigned int write(void *buffer,unsigned int size) = 0;
    };

public:
    explicit CompressorMotor(Adapter *adapter);
    virtual ~CompressorMotor();

    virtual bool buildPackets(uint8_t RW,uint8_t slave_address , uint16_t access_address , uint16_t access_data); 
    virtual bool checkResponse(void);
    virtual uint16_t processResponse(void);

    virtual bool enableMotor(void);  
    virtual bool disableMotor(void);   
    virtual bool open(void);
    virtual bool close(void);
    virtual bool setSpeed(uint16_t speed);
    virtual bool corotationMotor(void);
    virtual bool reversalMotor(void);  

    virtual uint16_t getControlMode(void);
    virtual uint16_t getControlCMD(void);
    virtual uint16_t getSpeedSet(void);
    virtual uint16_t getRunDirection(void);
    virtual uint16_t getSpeedActual(void);
    virtual uint16_t getBusVoltage(void);

    static CompressorMotor * getInstance();

protected:
    Adapter *adapter;

public:
/* 从机地址 */
typedef enum SlaveAddress{
    SlaveAddress_DefaultAddress = 0x01,
}SlaveAddress_t;

/* 功能码*/
typedef enum FunctionCode{
    FunctionCode_WriteFun = 0x06,
    FunctionCode_ReadFun = 0x03,
}FunctionCode_t;

/* 寄存器地址 */
typedef enum RegisterAddress{
    RegisterAddress_ControlMode = 1000,
    RegisterAddress_ControlCMD = 1001,
    RegisterAddress_SpeedSet = 1002,
    RegisterAddress_RunDirection = 1003,
    RegisterAddress_SpeedActual = 2000,
    RegisterAddress_FaultInfo_1 = 2001,
    RegisterAddress_FaultInfo_2 = 2002,
    RegisterAddress_FaultInfo_3 = 2003,
    RegisterAddress_BusVoltage = 2005,    
    RegisterAddress_CurrentOutput = 2006,  
    RegisterAddress_TemperatureEnvir = 2007,  
    RegisterAddress_TemperatureModule = 2008,  
}RegisterAddress_t;

/* 写入数据 */
typedef enum Data{
    Data_0 = 0x0000,
    Data_1 = 0x0001,
}Data_t;

/* 读取响应数据错误类型 */
typedef enum ResponseErrorType{
    ResponseErrorType_LengthError = 0xFFFF,       
    ResponseErrorType_AddressError = 0xFFFE,      
    ResponseErrorType_DataNumberError = 0xFFFD,
    ResponseErrorType_CRCError = 0xFFFC,
    ResponseErrorType_FunctionError = 0xFFFB,
    ResponseErrorType_DefaultError = 0xFFFA,
}ResponseErrorType_t;

/* 电机请求协议数据结构 */
typedef struct RequestProtocol{
    uint8_t slave_address;      //  从机地址
    uint8_t function_code;      //  功能码
    uint16_t access_address;    //  访问地址
    uint16_t access_data;       //  访问数据
    uint16_t crc;               //  CRC校验
}__attribute__((packed)) RequestProtocol_t;

/* 电机应答协议数据结构 */
typedef struct ResponseProtocol{
    uint8_t slave_address;      //  从机地址
    uint8_t function_code;      //  功能码
    uint8_t data_number;        //  数据个数
    uint16_t register_value;    //  从机寄存器值
    uint16_t crc;               //  CRC校验
}__attribute__((packed)) ResponseProtocol_t;

};

#endif

